package com.jiepos.mpos.biz.channel.service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jiepos.mpos.common.constant.DBConstant.Table;
import com.jiepos.mpos.common.constant.SystemConstant;
import com.jiepos.mpos.common.dao.HostLargeDao;
import com.jiepos.mpos.common.entity.App_Host_LargeEntity;
import com.jiepos.mpos.core.entity.Pagination;
import com.jiepos.mpos.core.service.BaseService;
import com.jiepos.mpos.core.util.IDFactoryUtil;
import com.jiepos.mpos.core.util.StringUtils;

/**
 * 
 * 
 * <p>标题: —— 大商户表服务类</p>
 * <p>模块: 通用平台</p>
 * <p>版权: Copyright (c) 2017 江苏杰博实信息技术有限公司
 * @ProjectName:  [mpos] 
 * @Package com.jiepos.mpos.biz.channel.service 
 * @author 张学涛
 * @version 1.0
 * @since 2017年9月27日下午2:18:25
 * @history（历次修订内容、修订人、修订时间等）
 */
@Service
public class HostLargeService extends BaseService {
	
	@Autowired
	private HostLargeDao hostLargeDao;	
	@Autowired
	private TransLimitService transLimitService;
	
	
	
	
	/**
	 * 查询大商户列表json分页数据
	 * @param pagination
	 * @param hostLarge
	 * @param ins_cd
	 * @param ins_cn_nm
	 * @param tablename
	 * @param channel_no
	 * @return
	 */
	public Map getHostLargeList(HttpServletRequest request,Pagination pagination,App_Host_LargeEntity hostLarge,
			String ins_cd,String ins_cn_nm,String tablename,String channel_no){
		StringBuffer sb = getInitSql("t1").append(",");
		sb.append("t1.host_large_uuid as uuid,t1.trf_mchnt_cd,t1.trf_term_id,t1.term_nm,NVL2 ( t2.ins_cd||t2.ins_cn_nm,t2.ins_cd||'-'||t2.ins_cn_nm, '') as insname,");
		sb.append("NVL2 ( t3.service_no||t3.service_name,t3.service_no||'-'||t3.service_name, '') as sername,NVL2 ( t4.param_no||t4.param_name,t4.param_no||'-'||t4.param_name, '') as purname,NVL2 ( t5.area_cd||t5.area_cn_nm,t5.area_cd||'-'||t5.area_cn_nm, '') as areaname,t1.acq_ins_cd,");
		sb.append("t1.master_key_idx,concat(rpad(substr( t1.master_key,1 , 6 ),length( t1.master_key )-4,'*') , substr( t1.master_key , length( t1.master_key)-3 , 4 ) ) as master_key,");		
		sb.append("NVL2 ( t6.channel_no||t6.chn_nm,t6.channel_no||'-'||t6.chn_nm, '')  as channel,to_char(t1.debit_minfee,'fm9999999990.00') as debit_minfee,to_char(t1.debit_rate,'fm9999999990.00') as debit_rate,to_char(t1.debit_maxfee,'fm9999999990.00') as debit_maxfee,to_char(t1.credit_minfee,'fm9999999990.00') as credit_minfee,");
		sb.append("to_char(t1.credit_rate,'fm9999999990.00') as credit_rate,to_char(t1.credit_maxfee,'fm9999999990.00') as credit_maxfee,NVL2 ( t7.code_val||t7.code_desc,t7.code_val||'-'||t7.code_desc, '')  as sign_st from ");
		sb.append(tablename.toUpperCase());
		sb.append(" t1 left join ");
		sb.append(Table.T_APP_INS_INF);
		sb.append(" t2 on t1.ins_uuid=t2.ins_uuid ");
		sb.append(" left join ");
		sb.append(Table.T_APP_SERVICE_INF);
		sb.append(" t3 on t1.service_uuid=t3.service_uuid ");
		sb.append(" left join ");
		sb.append(Table.T_APP_SERVICE_PARAM);
		sb.append(" t4 on t1.service_param_uuid=t4.service_param_uuid ");
		sb.append(" left join ");
		sb.append(Table.T_APP_AREA_CD);
		sb.append(" t5 on t1.area_cd=t5.area_cd ");
		sb.append(" left join ");
		sb.append(Table.T_APP_INF_CHANNEL);
		sb.append(" t6 on t6.channel_no='");
		sb.append(channel_no);
		sb.append("' left join ");
		sb.append(Table.T_APP_CODE_LIST);
		sb.append(" t7 on t1.sign_st=t7.code_val and t7.code_tp='SIGN_ST' ");
		sb.append(" where 1=1 ");
		List list = new ArrayList();
		if (!StringUtils.isBlank(hostLarge.getTrf_mchnt_cd())) {
			sb.append( " and t1.trf_mchnt_cd like ? " );
			list.add("%" + hostLarge.getTrf_mchnt_cd() + "%");
		}
		if (!StringUtils.isBlank(hostLarge.getTerm_nm())) {
			sb.append( " and t1.term_nm like ? " );
			list.add("%" + hostLarge.getTerm_nm() + "%");
		}
		if (!StringUtils.isBlank(ins_cd)) {
			sb.append( " and t2.ins_cd like ? " );
			list.add("%" + ins_cd + "%");
		}
		if (!StringUtils.isBlank(ins_cn_nm)) {
			sb.append( " and t2.ins_cn_nm like ? " );
			list.add("%" + ins_cn_nm + "%");
		}
		if (!StringUtils.isBlank(hostLarge.getSign_st())) {
			sb.append( " and t1.sign_st = ? " );
			list.add(hostLarge.getSign_st());
		}
		pagination.queryString = sb.toString();
		pagination = super.getInsAuthSql(request, pagination, "t2", null);
		pagination = this.getOrderSql(pagination, "t1");
		return hostLargeDao.getPaginationMap(pagination, "t1", list);
	}
	
	/**
	 * 保存大商户
	 * @param hostLarge
	 * @param map
	 * @throws Exception 
	 */
	public void saveHostLarge(App_Host_LargeEntity hostLarge,Map map,String tablename,String channel_no){	
		Map newHostLarge=null;
		List paramList = new ArrayList();	
		String masterkey=hostLarge.getMaster_key();		
		if(StringUtils.isBlank(hostLarge.getHost_large_uuid())){						
			paramList.add(IDFactoryUtil.getUUID());			
		}else{
			newHostLarge=hostLargeDao.getEntityById(tablename, hostLarge.getHost_large_uuid());
			if(masterkey.contains("*")){
				masterkey=String.valueOf(newHostLarge.get("master_key"));
			}
		}
		paramList.add(hostLarge.getTerm_nm());
		paramList.add(hostLarge.getTrf_mchnt_cd());
		paramList.add(hostLarge.getTrf_term_id());
		paramList.add(hostLarge.getIns_uuid());
		paramList.add(hostLarge.getService_uuid());
		paramList.add(hostLarge.getService_param_uuid());
		paramList.add(hostLarge.getArea_cd());
		paramList.add(hostLarge.getAcq_ins_cd());
		if(hostLarge.getMaster_key_idx()==null){
			paramList.add(0);	
		}else{			
			paramList.add(hostLarge.getMaster_key_idx());		
		}		
		paramList.add(masterkey);
		paramList.add(hostLarge.getMac_key());
		paramList.add(hostLarge.getPin_key());
		paramList.add(hostLarge.getTrk_key());
		paramList.add(hostLarge.getDebit_minfee());
		paramList.add(hostLarge.getDebit_rate());
		paramList.add(hostLarge.getDebit_maxfee());
		paramList.add(hostLarge.getCredit_minfee());
		paramList.add(hostLarge.getCredit_rate());
		paramList.add(hostLarge.getCredit_maxfee());
		paramList.add(hostLarge.getSign_st());		
		if (!StringUtils.isBlank(hostLarge.getHost_large_uuid())) {																					
			paramList.add(new Timestamp(System.currentTimeMillis()));
			paramList.add(String.valueOf(map.get(SystemConstant.SESSION_LOGIN_USER)));
			paramList.add(SystemConstant.OPER_UPDATE);			
			paramList.add(newHostLarge.get("rec_id"));
			hostLargeDao.updateEntity(paramList, tablename);
		}else{								
			paramList.add(SystemConstant.NORMAL_STATUS);
			paramList.add(new Timestamp(System.currentTimeMillis()));
			paramList.add(new Timestamp(System.currentTimeMillis()));
			paramList.add(String.valueOf(map.get(SystemConstant.SESSION_LOGIN_USER)));
			paramList.add(SystemConstant.OPER_ADD);			
			hostLargeDao.saveEntity(paramList, tablename);
			
			transLimitService.initTransLimit(channel_no, hostLarge.getTrf_mchnt_cd(),map);
		}
		
	}
	
	/**
	 * 根据记录id查询实体对象
	 * @param host_large_uuid
	 * @param tablename
	 * @param channel_no
	 * @return
	 */
	public Map getHostLargeDetail(String host_large_uuid,String tablename,String channel_no){
		if (StringUtils.isBlank(host_large_uuid) || host_large_uuid.equals("undefined")) {
			return new HashMap();
		}
		StringBuilder sb = new StringBuilder();
		sb.append("select t1.host_large_uuid,t1.trf_mchnt_cd,t1.trf_term_id,t1.term_nm,t1.ins_uuid,NVL2 ( t2.ins_cd||t2.ins_cn_nm,t2.ins_cd||'-'||t2.ins_cn_nm, '') as insname,");
		sb.append("t1.service_uuid,NVL2 ( t3.service_no||t3.service_name,t3.service_no||'-'||t3.service_name, '') as sername,t1.service_param_uuid,NVL2 ( t4.param_no||t4.param_name,t4.param_no||'-'||t4.param_name, '') as purname,t1.area_cd,NVL2 ( t5.area_cd||t5.area_cn_nm,t5.area_cd||'-'||t5.area_cn_nm, '') as areaname,t1.acq_ins_cd,");
		sb.append("t1.master_key_idx,concat(rpad(substr( t1.master_key,1 , 6 ),length( t1.master_key )-4,'*') , substr( t1.master_key , length( t1.master_key)-3 , 4 ) ) as master_key,");			
		sb.append("to_char(t1.debit_minfee,'fm9999999990.00') as debit_minfee,to_char(t1.debit_rate,'fm9999999990.00') as debit_rate,to_char(t1.debit_maxfee,'fm9999999990.00') as debit_maxfee,to_char(t1.credit_minfee,'fm9999999990.00') as credit_minfee,");
		sb.append("to_char(t1.credit_rate,'fm9999999990.00') as credit_rate,to_char(t1.credit_maxfee,'fm9999999990.00') as credit_maxfee,t1.sign_st,t1.rec_upd_usr_id from ");
		sb.append(tablename.toUpperCase());
		sb.append(" t1 left join ");
		sb.append(Table.T_APP_INS_INF);
		sb.append(" t2 on t1.ins_uuid=t2.ins_uuid ");
		sb.append(" left join ");
		sb.append(Table.T_APP_SERVICE_INF);
		sb.append(" t3 on t1.service_uuid=t3.service_uuid ");
		sb.append(" left join ");
		sb.append(Table.T_APP_SERVICE_PARAM);
		sb.append(" t4 on t1.service_param_uuid=t4.service_param_uuid ");
		sb.append(" left join ");
		sb.append(Table.T_APP_AREA_CD);
		sb.append(" t5 on t1.area_cd=t5.area_cd ");		
		sb.append(" where t1.rec_st!='");
		sb.append(SystemConstant.DEL_STATUS);
		sb.append("' and t1.host_large_uuid='");
		sb.append(host_large_uuid+"'");
		List<Map> list=hostLargeDao.queryForList(sb.toString());
		if(list==null||list.size()==0){
			return new HashMap();
		}else{
			return list.get(0);
		}		
		
	}
	
	/**
	 * 删除
	 * @param host_large_uuids
	 * @param tablename
	 */
	public void deleteHostLargeByIds(String host_large_uuids,String tablename){
		hostLargeDao.updateStatusById("host_large_uuid", host_large_uuids, tablename);
	}
	
	/**
	 * 校验
	 * @param hostLarge
	 * @param map
	 * @param tablename
	 * @param channel_no
	 * @return
	 */
	public String checkHostLarge(App_Host_LargeEntity hostLarge,String tablename){
		StringBuilder sb = new StringBuilder();
		sb.append("select host_large_uuid,term_nm,trf_mchnt_cd,trf_term_id,ins_uuid,service_uuid,service_param_uuid,area_cd from ");		
		sb.append(tablename);
		sb.append(" where 1=1 ");
		List list = new ArrayList();
		if (!StringUtils.isBlank(hostLarge.getTrf_mchnt_cd())) {
			sb.append( " and trf_mchnt_cd = ? " );
			list.add(hostLarge.getTrf_mchnt_cd());
		}			
		if (!StringUtils.isBlank(hostLarge.getHost_large_uuid())) {
			sb.append( " and host_large_uuid != ? " );
			list.add(hostLarge.getHost_large_uuid());
		}	
		List result=hostLargeDao.queryForList(sb.toString(), list);
		if(result==null||result.size()==0){
			return SystemConstant.SUCCESS;
		}
		return SystemConstant.ERROR;
	}

}
